package org.tp8.sampleapp.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.tp8.sampleapp.service.facade.BookDocFacade;
import org.tp8.sampleapp.service.facade.CourseFacade;
import org.tp8.sampleapp.service.facade.StudentFacade;
import org.tp8.sampleapp.service.facade.TeacherFacade;

/**
 * The Class AbstractBean.
 */
public abstract class AbstractBean implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6489999685157943466L;

	/**
	 * The teacher facade.
	 */
	@ManagedProperty(value = "#{teacherFacade}")
	private TeacherFacade teacherFacade;

	/**
	 * The course facade.
	 */
	@ManagedProperty(value = "#{courseFacade}")
	private CourseFacade courseFacade;

	/**
	 * The student facade.
	 */
	@ManagedProperty(value = "#{studentFacade}")
	private StudentFacade studentFacade;

	/** The book facade. */
	@ManagedProperty(value = "#{bookFacade}")
	private BookDocFacade bookFacade;

	/** The bundle. */
	private ResourceBundle bundle;

	/**
	 * Gets the teacher facade.
	 * 
	 * @return the teacher facade
	 */
	public TeacherFacade getTeacherFacade() {
		return teacherFacade;
	}

	/**
	 * Sets the teacher facade.
	 * 
	 * @param teacherFacade
	 *            the new teacher facade
	 */
	public void setTeacherFacade(TeacherFacade teacherFacade) {
		this.teacherFacade = teacherFacade;
	}

	/**
	 * Gets the course facade.
	 * 
	 * @return the course facade
	 */
	public CourseFacade getCourseFacade() {
		return courseFacade;
	}

	/**
	 * Sets the course facade.
	 * 
	 * @param courseFacade
	 *            the new course facade
	 */
	public void setCourseFacade(CourseFacade courseFacade) {
		this.courseFacade = courseFacade;
	}

	/**
	 * Gets the student facade.
	 * 
	 * @return the student facade
	 */
	public StudentFacade getStudentFacade() {
		return studentFacade;
	}

	/**
	 * Sets the student facade.
	 * 
	 * @param studentFacade
	 *            the new student facade
	 */
	public void setStudentFacade(StudentFacade studentFacade) {
		this.studentFacade = studentFacade;
	}

	/**
	 * Gets the book facade.
	 * 
	 * @return the bookFacade
	 */
	public BookDocFacade getBookFacade() {
		return bookFacade;
	}

	/**
	 * Sets the book facade.
	 * 
	 * @param bookFacade
	 *            the bookFacade to set
	 */
	public void setBookFacade(BookDocFacade bookFacade) {
		this.bookFacade = bookFacade;
	}

	/**
	 * Gets the message.
	 * 
	 * @param key
	 *            the key
	 * @return the message
	 */
	public String getMessage(String key) {
		if (bundle == null) {
			bundle = ResourceBundle.getBundle("messages.messages", FacesContext
					.getCurrentInstance().getViewRoot().getLocale());
		}
		String value = bundle.getString(key);
		return value;
	}

	/**
	 * Gets the external context.
	 * 
	 * @return the external context
	 */
	public ExternalContext getExternalContext() {
		return FacesContext.getCurrentInstance().getExternalContext();
	}

	/**
	 * Gets the session map.
	 * 
	 * @return the session map
	 */
	public Map<String, Object> getSessionMap() {
		return getExternalContext().getSessionMap();
	}
	
	/**
	 * Gets the request map.
	 * 
	 * @return the request map
	 */
	public Map<String, Object> getRequestMap() {
		return getExternalContext().getRequestMap();
	}
	
	/**
	 * Gets the request parameter map.
	 * 
	 * @return the request parameter map
	 */
	public Map<String, String> getRequestParameterMap() {
		return getExternalContext().getRequestParameterMap();
	}

	/**
	 * Gets the session.
	 * 
	 * @return the session
	 */
	public HttpSession getSession() {
		return (HttpSession) getExternalContext().getSession(true);
	}

	/**
	 * Gets the request.
	 * 
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return (HttpServletRequest) getExternalContext().getRequest();
	}

	/**
	 * Gets the response.
	 * 
	 * @return the response
	 */
	public HttpServletResponse getResponse() {
		return (HttpServletResponse) getExternalContext().getResponse();
	}

	/**
	 * Gets the session param.
	 * 
	 * @param key
	 *            the key
	 * @return the session param
	 */
	public Object getSessionParam(String key) {
		return getSessionMap().get(key);
	}
	
	/**
	 * Gets the request param.
	 * 
	 * @param key
	 *            the key
	 * @return the request param
	 */
	public Object getRequestParam(String key) {
		return getRequestParameterMap().get(key);
	}

	/**
	 * Redirect.
	 * 
	 * @param url
	 *            the url
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public void redirect(String url) throws IOException {
		getResponse().sendRedirect(url);
	}

}
